/*
 * Logout
 * 
 * Version information
 * 
 * Apr 10, 2006
 * 
 * Copyright (c) AmSoft Systems, 2006
 */
package net.amsoft.iservice.management.webapp.struts.action;

import static net.amsoft.iservice.util.IServiceConstants.DEBUG;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.amsoft.iservice.management.util.IBrokerConstants;
import net.amsoft.iservice.management.webapp.dataObject.LoginData;
import net.amsoft.iservice.isso.client.ISSOClient;
import net.amsoft.iservice.isso.client.SAMLDataObject;
import net.amsoft.iservice.util.IServiceConstants;
import net.amsoft.iservice.util.IServiceUtil;
import net.amsoft.iservice.util.MetaData;
import net.amsoft.iservice.util.ResolverClient;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 * @author rahul
 * 
 */
public class Logout extends BaseAction {

    public ActionForward process(ActionMapping oMapping, ActionForm oForm,
            HttpServletRequest oRequest, HttpServletResponse oResponse)
            throws Exception {

        IServiceUtil.log(IServiceConstants.INFO, this, F_PROCESS + F_ENTRY);
        LoginData oLoginData = (LoginData) oRequest.getSession(false)
                .getAttribute(IBrokerConstants.SESSION_LOGIN_OBJ);
        ActionMessages oMsg = new ActionErrors();
        if (oLoginData.isLoginStatus() == false) {
            IServiceUtil.log(IServiceConstants.WARN, this, F_PROCESS + "already logged out");
            IServiceUtil.log(DEBUG, this, F_PROCESS + "redirect to=" + 
            		IBrokerConstants.FWD_CHKLOGIN);
            return oMapping.findForward(IBrokerConstants.FWD_CHKLOGIN);
        }

        SAMLDataObject oSAMLData = oLoginData.getSAMLData();
        String sLoggedInSynonym = oLoginData.getLoggedInSynonym();
        String sSPXri = IBrokerConstants.getSPXri(oRequest.getServerName());
        MetaData oMetaData = ResolverClient.getFinalMetaData(sLoggedInSynonym);
        if (oMetaData == null) {
            oRequest.getSession(false).invalidate();
            IServiceUtil.log(IServiceConstants.WARN, this, F_PROCESS + "invalid metadata");
            oMsg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                    IBrokerConstants.ERR_GLOBAL_LOGOUT_NOT_DONE));
            saveErrors(oRequest, oMsg);
            IServiceUtil.log(DEBUG, this, F_PROCESS + "redirect to=" + 
            		IBrokerConstants.FWD_CHKLOGIN);
            return oMapping.findForward(IBrokerConstants.FWD_CHKLOGIN);
        }
        String sProviderID = oMetaData.getProviderID();
        String sRelayState = IServiceUtil.generateUniqueID();
        String sLogoutRequestXML = ISSOClient.createSAMLLogoutRequest(
                sLoggedInSynonym, oSAMLData.getSessionIndex(), sSPXri,
                oMetaData.getISLOUrl());

        ActionForward oFwd = new ActionForward(ISSOClient
                .getRedirectRequestURI(oMetaData.getISLOUrl(),
                        sLogoutRequestXML, sRelayState), true);

        oLoginData = new LoginData();
        oLoginData.setRelayState(sRelayState);
        oLoginData.setProviderID(sProviderID);
        oLoginData.setProviderIDHash(IServiceUtil
                .getOneWayEncValue(sProviderID));
        oLoginData.setIDPMetaDataEP(oMetaData.getServiceEndPoint());
        oRequest.getSession(false).setAttribute(IBrokerConstants.SESSION_LOGIN_OBJ, oLoginData);

        IServiceUtil.log(IServiceConstants.INFO, this, F_PROCESS + 
                "sending logout request at: " + oFwd.getPath());
        return oFwd;
    }
}
